package controller

// 用于普通用户处理账本客户端
import (
	"fabric-go-sdk/sdk"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 查询区块链信息
func GetBlockchainInfo(c *gin.Context) {
	// 获取fabric身份信息
	channelId := c.Query("channelId")
	orgName := c.Query("orgName")
	userName := c.Query("userName")
	fmt.Println("=== GetBlockchainInfo channelId==", channelId)
	fmt.Println("=== GetBlockchainInfo orgName==", orgName)
	fmt.Println("=== GetBlockchainInfo userName==", userName)

	// 查询区块信息
	blockchainInfo, err := sdk.App.GetBlockchainInfo(channelId, orgName, userName)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "SDK调用失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	//查询配置信息
	configInfo, err := sdk.App.GetConfigInfo(channelId, orgName, userName)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "SDK调用失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	_, sum, err := sdk.App.GetTxHashList(channelId, orgName, userName)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "SDK调用失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	// 2. 返回响应
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "成功",
		"success": true,
		"data": gin.H{
			"blockNum":    int(blockchainInfo.BCI.Height - 1),
			"txNum":       sum,
			"channelName": configInfo.ID(),
			"orderers":    configInfo.Orderers(),
			"peerNum":     len(configInfo.MSPs()),
		},
	})
}

// 查询区块信息
func GetBlockInfo(c *gin.Context) {
	// 解析fabric身份信息
	channelId := c.Query("channelId")
	orgName := c.Query("orgName")
	userName := c.Query("userName")
	fmt.Println("=== GetBlockchainInfo channelId==", channelId)
	fmt.Println("=== GetBlockchainInfo orgName==", orgName)
	fmt.Println("=== GetBlockchainInfo userName==", userName)
	blockNum, err := strconv.Atoi(c.Query("blocknum"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "输入的区块号有问题,err：" + err.Error(),
			"success": false,
		})
		return
	}
	// 查询
	blockInfo, err := sdk.App.GetBlockInfo(channelId, orgName, userName, blockNum)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "调用SDK失败,err：" + err.Error(),
			"success": false,
		})
		return
	}
	//a:= common.Envelope()
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "成功",
		"success": true,
		"data":    blockInfo,
	})

}

// 获取最新的五个区块
func GetLastFiveBlock(c *gin.Context) {
	// 解析fabric身份信息
	channelId := c.Query("channelId")
	orgName := c.Query("orgName")
	userName := c.Query("userName")
	fmt.Println("=== GetBlockchainInfo channelId==", channelId)
	fmt.Println("=== GetBlockchainInfo orgName==", orgName)
	fmt.Println("=== GetBlockchainInfo userName==", userName)
	// 查询
	blocklist, err := sdk.App.GetLastFiveBlock(channelId, orgName, userName)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "SDK连接失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "成功",
		"success": true,
		"data":    blocklist,
	})

}

func GetBlocksPage(c *gin.Context) {
	// 获取参数
	channelId := c.Query("channelId")
	orgName := c.Query("orgName")
	userName := c.Query("userName")
	fmt.Println("=== GetBlockchainInfo channelId==", channelId)
	fmt.Println("=== GetBlockchainInfo orgName==", orgName)
	fmt.Println("=== GetBlockchainInfo userName==", userName)
	page, err := strconv.Atoi(c.Param("page"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	limit, err := strconv.Atoi(c.Param("limit"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	// 查询
	blocklist, blockSum, pages, err := sdk.App.GetBlockByPage(channelId, orgName, userName, page, limit)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "成功",
		"success": true,
		"data": gin.H{
			"records": blocklist,
			"total":   blockSum, //总区块数
			"size":    limit,    //每页区块数==limit
			"current": page,     //当前页数===page
			"pages":   pages,    //总页数
		},
	})
}

func GetTxsPage(c *gin.Context) {
	// 获取参数
	channelId := c.Query("channelId")
	orgName := c.Query("orgName")
	userName := c.Query("userName")
	fmt.Println("=== GetBlockchainInfo channelId==", channelId)
	fmt.Println("=== GetBlockchainInfo orgName==", orgName)
	fmt.Println("=== GetBlockchainInfo userName==", userName)
	page, err := strconv.Atoi(c.Param("page"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "失败，err:" + err.Error(),
			"success": false,
		})
		return
	}
	limit, err := strconv.Atoi(c.Param("limit"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "失败，err:" + err.Error(),
			"success": false,
		})
		return
	}
	txList, txSum, pages, err := sdk.App.GetTxByPage(channelId, orgName, userName, page, limit)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "失败，err:" + err.Error(),
			"success": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "成功",
		"success": true,
		"data": gin.H{
			"records": txList,
			"total":   txSum, //总交易数
			"size":    limit, //每页交易数==limit
			"current": page,  //当前页数===page
			"pages":   pages, //总页数
		},
	})
}

func GetLatestTx(c *gin.Context) {
	// 获取参数
	channelId := c.Query("channelId")
	orgName := c.Query("orgName")
	userName := c.Query("userName")
	fmt.Println("=== GetBlockchainInfo channelId==", channelId)
	fmt.Println("=== GetBlockchainInfo orgName==", orgName)
	fmt.Println("=== GetBlockchainInfo userName==", userName)
	// 查询
	txList, err := sdk.App.GetLastestTx(channelId, orgName, userName)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "失败,err:" + err.Error(),
			"success": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "成功",
		"success": true,
		"data":    txList,
	})
}
